<template>
  <!-- 我的地址页 -->
  <div class="box">
    <div class="top">
      <span class="iconfont icon-chevron-thin-left" @click="getlog()"></span>
      <b>我的收货地址</b>
    </div>
    <div class="asgd">
       <van-address-list
        v-model="chosenAddressId"
        :list="list"
        @add="onAdd"
        @edit="onEdit"
        @select="checkos"
        add-button-text="+ 新增地址"
        default-tag-text="默认"
      />
    </div>
  </div>
</template>
<script>
import * as API from "@/components/api/hanjinqi.js";
export default {
  async created() {
    //获取当前用户的地址
    let res = await API.sitetgh({
      uid: JSON.parse(localStorage.getItem("token")).uid,
    });
    // console.log(res.data)
    res.data.forEach((item, index) => {
      item.isDefault = JSON.parse(item.isDefault);
      if (item.isDefault) {
        this.chosenAddressId = item.id;
      }
    });
    this.list = res.data;
  },
  data() {
    return {
      chosenAddressId: 1,
      list: [
        // {
        //   id: "1",
        //   name: "张三",
        //   tel: "13000000000",
        //   address: "浙江省杭州市西湖区文三路 138 号东方通信大厦 7 楼 501 室",
        //   // isDefault: true,
        // },
        // {
        //   id: "2",
        //   name: "李四",
        //   tel: "1310000000",
        //   address: "浙江省杭州市拱墅区莫干山路 50 号",
        // },
      ],
    };
  },
  methods: {
    //   点击返回上一页
    getlog() {
      this.$router.go(-1);
    },
    // 点击新增收货地址
    onAdd() {
      this.$router.push({ path: "/xinsite" });
    },
    // 点击编辑
    onEdit(item, index) {
      // this.$router.push({path:'/Luyou/fenlei',query:{plan:this.list}})
      this.$router.push({ path: "/xinsite", query: { plan: item } });
      // console.log(item);
      // console.log(index);
    },
    // 点击单选框
    async checkos(item) {
      let res = await API.hsiteselect({
        uid: JSON.parse(localStorage.getItem("token")).uid,
        id: item.id,
      });
      res.data.forEach((item, index) => {
        item.isDefault = JSON.parse(item.isDefault);
        if (item.isDefault) {
          this.chosenAddressId = item.id;
        }
      });
      this.list = res.data;
    },
  },
};
</script>

<style scoped>
.box {
  width: 100%;
  /* height: 830px; */
  height: 100vh;
  z-index: 666;
  position: fixed;
  left: 0;
  top: 0;
  background: #f3f3f3;
}
.box .top {
  width: 100%;
  height: 4rem;
  background: #FFFFFF;

}
.top span {
  width: 6%;
  float: left;
  line-height: 4rem;
  margin-left: 0.3rem;
}
.top b {
  /* margin-left: 0.5rem; */
  line-height: 4rem;
  font-weight: 600;
}
.top p {
  float: right;
  line-height: 4rem;
  margin-right: 1rem;
  color: #e05b15;
}
.box .asgd {
  width: 100%;
  height: 100%;
  background: #f0eeee;
  /* border-radius: 0.6rem; */
} 

</style>